package com.meizu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.meizu.dao.RoleDao;
import com.meizu.dao.UserRepository;
import com.meizu.entity.Role;
import com.meizu.entity.User;
import com.meizu.service.IHibernateJpaService;

@Service
public class HibernateJpaServiceImpl implements IHibernateJpaService{
	@Autowired
	private UserRepository UserRepository; 

	@Autowired
	private RoleDao roleDao;
	
	@Override
	public void testHibernateJpaTransaction() {
		User user = new User();
		user.setName("test");
		user.setPass("123456");
		UserRepository.save(user);
		User user2 = new User();
		user2.setName("test");
		user2.setPass("123456");
		UserRepository.save(user2);
		User user3 = new User();
		user3.setName("test");
		user3.setPass("123456");
		UserRepository.save(user3);
		
		String name = "test";
		List<User> list = UserRepository.findByName(name);
		//利用jpa构建分页查询
		//排序对象
		Sort sort = new Sort(Direction.DESC, "id");
		//通过PageRequest构造Pageable对象
		Pageable pageable = new PageRequest(1, 2, sort);
		Page<User> page = UserRepository.findAll(pageable);
	}

	@Override
	public List<User> testQuery() {
		// TODO Auto-generated method stub
		Role role = new Role(); 
		role.setCode("123");
		role.setName("role1");
		roleDao.save(role);
		String name = "test";
		List<User> uList = UserRepository.findByOrgName(name);
		System.out.println(uList.size());
		return uList;
	}

}
